//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// PrintChartView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "PrintChartView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPrintChartView

IMPLEMENT_DYNCREATE(CPrintChartView, CBCGPChartView)

CPrintChartView::CPrintChartView()
{
	m_bIsReady = FALSE;
}

CPrintChartView::~CPrintChartView()
{
}


BEGIN_MESSAGE_MAP(CPrintChartView, CBCGPChartView)
	//{{AFX_MSG_MAP(CPrintChartView)
	ON_COMMAND(ID_CHART_COPY, OnChartCopy)
	ON_COMMAND(ID_CHART_EXPORT, OnChartExport)
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI(ID_ANIMATION_COMBO, OnUpdateAnimationStyleCombo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrintChartView diagnostics

#ifdef _DEBUG
void CPrintChartView::AssertValid() const
{
	CBCGPChartView::AssertValid();
}

void CPrintChartView::Dump(CDumpContext& dc) const
{
	CBCGPChartView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPrintChartView message handlers

void CPrintChartView::OnInitialUpdate() 
{
	CBCGPChartView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	EnableAdvancedLegend();

	if (GetChartCtrl() == NULL)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = GetChartCtrl()->GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartLine);

	pChart->SetChartTitle(_T("Furniture Sales"));

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Tables"));
	CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Chairs"));
	CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("Cabinets"));

	pSeries1->AddDataPoint(5.);
	pSeries1->AddDataPoint(8.);
	pSeries1->AddDataPoint(12.);
	pSeries1->AddDataPoint(13.);
	pSeries1->AddDataPoint(15.);

	pSeries2->AddDataPoint(9.);
	pSeries2->AddDataPoint(15.);
	pSeries2->AddDataPoint(19.);
	pSeries2->AddDataPoint(21.);
	pSeries2->AddDataPoint(23.);

	pSeries3->AddDataPoint(2.);
	pSeries3->AddDataPoint(3.);
	pSeries3->AddDataPoint(16.);
	pSeries3->AddDataPoint(19.);
	pSeries3->AddDataPoint(19.);

	pChart->ShowDataMarkers();
	pChart->ShowDataLabels();

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartSeries* pSeries = pChart->GetSeries(i);
		ASSERT_VALID(pSeries);

		BCGPChartFormatSeries style = pSeries->GetSeriesFormat();

		style.m_curveType = BCGPChartFormatSeries::CCT_SPLINE;
		style.SetSeriesLineWidth(theApp.GetDefaultLineWidth());

		pSeries->SetSeriesFormat(style);
	}
	
	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	UpdateChartColorTheme(pMainFrame->GetColorTheme(), pMainFrame->IsDarkTheme());

	pChart->Redraw();
}

void CPrintChartView::OnChartCopy() 
{
	if (GetChartCtrl() == NULL)
	{
		return;
	}

	GetChartCtrl()->CopyToClipboard();
}

void CPrintChartView::OnChartExport() 
{
	if (GetChartCtrl() == NULL)
	{
		return;
	}

	CFileDialog dlg (FALSE, _T("bmp"), _T(""), OFN_HIDEREADONLY, 
		_T("Bitmap Files (*.bmp)|*.bmp|Png Files (*.png)|*.png|All Files (*.*)|*.*||"), this);
	if (dlg.DoModal () == IDOK)
	{
		GetChartCtrl()->ExportToFile(dlg.GetPathName ());
	}
}

void CPrintChartView::OnUpdateAnimationStyleCombo(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(FALSE);	
}
